function setLocale(lang: string) {
  if (lang !== undefined && !/^([a-z]{2})-([A-Z]{2})$/.test(lang)) {
    throw new Error('setLocale lang format error');
  }

  if (typeof window !== 'undefined' && getLocale() !== lang) {
    window.localStorage.setItem('lang', lang);
    window.location.reload();
  }
}

export function getUrlToken(name, str) {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
  const r = str.substr(1).match(reg);
  if (r != null) return decodeURIComponent(r[2]);
  return null;
}



export function datetimeStamp2String(nS) {
  // eslint-disable-next-line radix
  return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}


function getLocale() {
  if (typeof window !== 'undefined') {
    if (!window.localStorage.getItem('lang')) {
      window.localStorage.setItem('lang', navigator.language);
    }
    return localStorage.getItem('lang');
  }
  return '';
}

export {setLocale, getLocale};
